CREATE PUBLICATION
CREATE PUBLICATION — Define a new publication
Synopsis
CREATE PUBLICATION name
[ FOR TABLE [ ONLY ] table_name [ * ] [, ...]
| FOR ALL TABLES ]
[ WITH ( publication_parameter [= value] [, ... ] ) ]
Description
CREATE PUBLICATION adds a new publication to the current database. The publication name must be distinct from the names of any existing publications in the current database.
A publication is essentially a set of tables whose data changes are intended to be replicated through logical replication.
Parameters
name
The name of the new publication.
FOR TABLE
Specifies a list of tables to add to the publication. If ONLY is specified before the table name, only that table is added to the publication. If ONLY is not specified, the table and all its descendants (if any) are added. Optionally, * can be specified after the table name to explicitly indicate that descendants are included. However, this does not apply to partitioned tables. Partitions of a partitioned table are always implicitly considered part of the publication, so they are never explicitly added to the publication.
Only persistent base tables and partitioned tables can be part of a publication. Temporary tables, unlogged tables, foreign tables, materialized views, and regular views cannot be part of a publication.
When a partitioned table is added to a publication, all of its existing and future partitions are implicitly considered part of the publication. Therefore, even operations performed directly on a partition will be published through the publication that its ancestor belongs to.
FOR ALL TABLES
Marks the publication as replicating changes to all tables in the database, including tables created in the future.
WITH ( publication_parameter [= value] [, ... ] )
This clause specifies optional parameters for the publication. The following parameters are supported:
publish (string)
This parameter determines which DML operations will be published by the new publication to subscribers. The value is a comma-separated list of operations. Allowed operations are insert, update, delete, and truncate. The default is to publish all actions, so the default value for this option is 'insert, update, delete, truncate'.
publish_via_partition_root (boolean)
This parameter determines whether changes in a partitioned table (or its partitions) included in the publication will be published using the identity and schema of the partitioned table rather than the identity and schema of the individual partition where the change actually occurred. The default is to use the identity and schema of the individual partition. Enabling this allows changes to be replicated to a non-partitioned table or a partitioned table composed of a different set of partitions.
If this is enabled, TRUNCATE operations performed directly on partitions will not be replicated.
Note
If neither FOR TABLE nor FOR ALL TABLES is specified, the publication starts with an empty set of tables. This is useful when tables will be added later.
Creating a publication does not start replication. It only defines grouping and filtering logic for future subscribers.
To create a publication, the caller must have CREATE privilege on the current database. (Of course, superusers bypass this check.)
To add a table to a publication, the caller must own the table. The FOR ALL TABLES clause requires the caller to be a superuser.
Tables added to a publication that publishes UPDATE and/or DELETE operations must have REPLICA IDENTITY defined. Otherwise, these operations will be prohibited on those tables.
For INSERT ... ON CONFLICT commands, the publication publishes the operation that actually results from the command. Therefore, depending on the outcome, it may be published as INSERT or UPDATE, or not published at all.
COPY ... FROM commands are published as INSERT operations.
DDL operations are not published.
Examples
# Create a publication that publishes all changes in two tables:
CREATE PUBLICATION mypublication FOR TABLE users, departments;
# Create a publication that publishes all changes in all tables:
CREATE PUBLICATION alltables FOR ALL TABLES;
# Create a publication that only publishes INSERT operations from one table:
CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert');